import fa from "element-ui/src/locale/lang/fa";

export default {
    data(){
        return {
            tableData:[],
            sendData:{},
            gertTableDataDelay:false
        }
    },
    mounted() {
        !this.gertTableDataDelay && this.getTableData()
    },
    methods:{
        showConfirm(label){
            return new Promise((resolve,reject) => {
                this.$alert(
                    `<strong style="color:#f56c6c;">${label}</strong>`,
                    '提示',
                    {
                        dangerouslyUseHTMLString: true,
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }
                ).then(() => {
                    resolve(true)
                }).catch()
            })
        },
        rowDelete(data,label,isAsk){
            return new Promise((resolve,reject) => {
                if(isAsk){
                    this.showConfirm(label).then(() => {
                        this.rowDeleteApi(data).then(res=>{
                            this.$message.success(res.msg)
                            resolve(res)
                            this.getTableData()
                        })
                    }).catch();
                }else {
                    this.rowDeleteApi(data).then(res=>{
                        this.$message.success(res.msg)
                        resolve(res)
                        this.getTableData()
                    })
                }
            })
        },
        getTableData(data){
            this.tableApi(data || this.sendData).then(res=>{
                this.tableData=res.data
                this.tableCallback && this.tableCallback()
            })
        },
        rowSave(data,isVerify,label){
            return new Promise((resolve,reject) => {
                if(isVerify){
                    this.$refs.commonValidateForm.validate((valid) => {
                        if(valid){
                            if(label){
                                this.showConfirm(label).then(res=>{
                                    this.rowSaveApi(data || this.sendFormData).then(res=>{
                                        this.rowSaveCallback && this.rowSaveCallback()
                                        this.$message.success(res.msg)
                                        this.visible && (this.visible=false);
                                        this.$refs.commonValidateForm.resetFields()
                                        resolve(res)
                                        this.getTableData()
                                    }).catch(err=>{
                                        reject(err)
                                    })
                                })
                            }else {
                                this.rowSaveApi(data || this.sendFormData).then(res=>{
                                    this.rowSaveCallback && this.rowSaveCallback()
                                    this.$message.success(res.msg)
                                    this.visible && (this.visible=false);
                                    this.$refs.commonValidateForm.resetFields()
                                    resolve(res)
                                    this.getTableData()
                                }).catch(err=>{
                                    reject(err)
                                })
                            }
                        }
                    })
                }else {
                    this.rowSaveApi(data || this.sendFormData).then(res=>{
                        // this.rowSaveCallback && this.rowSaveCallback()
                        this.$message.success(res.msg)
                        resolve(res)
                        this.getTableData()
                    }).catch(err=>{
                        reject(err)
                    })
                }
            })
        }
    }
}